<?php
namespace Admin\Controller;

use Think\Controller;

class CommenController extends Controller
{
    protected $log;
    static $redis;
    public function __construct()
    {
        parent::__construct();
        if (empty($_SESSION['id']) || (empty($_SESSION['xycshop'.$_SESSION['account']]) && $_SESSION['rid']!='99') || empty($_COOKIE['xycshop'.$_SESSION['account']])) {
//            if($_SESSION['account']=='15658700036'){
//                var_dump($_SESSION);
//                var_dump($_COOKIE);
//                die();
//            }
            session(null);
            cookie(null);
            $this->redirect('Login/login');
        };

        $admin = M('shopkeeper', '', 'XYC')->WHERE(['id' => $_SESSION['id'], 'state' => '1'])->FIND();
        if ((empty($admin) || $admin['state'] != '1') && $_SESSION['rid'] != '99') {
            session(null);
            $this->error('您的账号异常,暂时无法登录', U('Login/login'));
        }

        $auth = explode('|', $_SESSION['xycshop'.$_SESSION['account']]);
        $admin['top_id'] = $auth['0'];
        $admin['next_id'] = $auth['1'];
        $admin['last_id'] = $auth['2'];
        $controller = strtolower(CONTROLLER_NAME);
        $action = strtolower(ACTION_NAME);
        $controller_id = M('merchant_power', '', 'XYC')->WHERE(['actionname' => $controller, 'level' => '1', 'status' => '1','version'=>'2'])->getField('id');
        $action_id = M('merchant_power', '', 'XYC')->WHERE(['actionname' => $action, 'level' => ['gt', '1'], 'status' => '1','version'=>'2'])->getField('id');
        if ($_SESSION['rid'] != '99' && $controller != 'index') {
            if (in_array($controller_id, explode(',', $admin['top_id']))) {
                if (!in_array($action_id, explode(',', $admin['next_id'])) && !in_array($action_id, explode(',', $admin['last_id']))) {
                    $this->error('您无访问权限');
                }
            } else {
                $this->error('您无访问权限');
            }
        }
    }


    /**
     * 获取星期一到星期日的时间戳
     */
    public function mon_sunday()
    {
        $arr[0] = time() - ((date('w') == 0 ? 7 : date('w')) - 1) * 24 * 3600;
        $arr[1] = time() - ((date('w') == 0 ? 7 : date('w')) - 2) * 24 * 3600;
        $arr[2] = time() - ((date('w') == 0 ? 7 : date('w')) - 3) * 24 * 3600;
        $arr[3] = time() - ((date('w') == 0 ? 7 : date('w')) - 4) * 24 * 3600;
        $arr[4] = time() - ((date('w') == 0 ? 7 : date('w')) - 5) * 24 * 3600;
        $arr[5] = time() - ((date('w') == 0 ? 7 : date('w')) - 6) * 24 * 3600;
        $arr[6] = time() - ((date('w') == 0 ? 7 : date('w')) - 7) * 24 * 3600;
        foreach ($arr as $k => $v) {
            $start = mktime('0', '0', '0', date('m', $v), date('d', $v), date('Y', $v));
            $end = mktime('23', '59', '59', date('m', $v), date('d', $v), date('Y', $v));
            $time[] = array($start, $end);
        }
        return $time;
    }

    public function _empty()
    {
        $this->redirect('Alerterror/error404');
    }


}